﻿using AutoMapper;
using MediatR;
using Provenance.ErrorCount;
using Provenance.Infra.Interfaces.Product;
using Provenance.Read.Api.Application.Command.Product;
using Provenance.Read.Api.Dto.Product;

namespace Provenance.Read.Api.Application.CommandHandler.Product
{
    public class GetPictureInfoCommandHandler : IRequestHandler<GetPictureInfoCommand, ApiResult<IList<GetPictureInfoResponseDTO>>>
    {
        private readonly IPictureInfoRepository _pictureInfoRepository;
        private readonly ILogger<GetPictureInfoCommandHandler> _logger;
        private readonly IMapper _mapper;
        public GetPictureInfoCommandHandler(IPictureInfoRepository pictureInfoRepository, ILogger<GetPictureInfoCommandHandler> logger, IMapper mapper)
        {
            _pictureInfoRepository = pictureInfoRepository;
            _logger = logger;
            _mapper = mapper;
        }
        public async Task<ApiResult<IList<GetPictureInfoResponseDTO>>> Handle(GetPictureInfoCommand request, CancellationToken cancellationToken)
        {
            try
            {
                var list = await _pictureInfoRepository.GetAllAsync(x => x.ProductInfoId == request.ProductInfoId && x.TraceType == request.TraceType && x.ImageType == request.ImageType);
                var result = _mapper.Map<IList<GetPictureInfoResponseDTO>>(list);
                return ApiResult<IList<GetPictureInfoResponseDTO>>.Success(result);
            }
            catch (Exception ex)
            {
                _logger.LogError("Error getting picture info" + ex.Message);
                return ApiResult<IList<GetPictureInfoResponseDTO>>.Failure("Error getting picture info"+ ex.Message);
            }
        }
    }
}
